package com.click.app.webapp.action;

import com.click.app.model.AsistanceControl;
import com.click.app.model.Employee;
import com.click.app.model.LabelValue;
import com.click.app.model.Option;
import com.click.app.model.Role;
import com.click.app.model.Salary;
import com.click.app.model.User;
import com.click.app.service.AsistanceControlManager;
import com.click.app.service.EmployeeManager;
import com.click.app.service.SalaryManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author click
 */
public class MenuAction extends BaseAction {

    private EmployeeManager employeeManager;
    private SalaryManager salaryManager;
    private AsistanceControlManager asistanceControlManager;

    public void setEmployeeManager(EmployeeManager employeeManager) {
        this.employeeManager = employeeManager;
    }

    public void setSalaryManager(SalaryManager salaryManager) {
        this.salaryManager = salaryManager;
    }

    public void setAsistanceControlManager(AsistanceControlManager asistanceControlManager) {
        this.asistanceControlManager = asistanceControlManager;
    }

    @Override
    public String execute() {

        User u = userManager.getUserByUsername(getRequest().getRemoteUser());

        if (u.isUserAdminPayment()) {
            for (Employee e : employeeManager.findByRole("ROLE_EMPLOYEE_FIXED")) {
                if (!e.getFinishSalaries()) {
                    List<Salary> ss = salaryManager.findByEmployee(e);
                    if (ss.size() > 0) {
                        Salary s = ss.get(ss.size() - 1);
                        Date currentDate = new Date(System.currentTimeMillis());
                        Date endDate = new Date(s.getEndDate().getTime());
                        int aCLSize = asistanceControlManager.sizeByEmployeePeriod(e, s.getInitDate(), endDate);
                        int difACLSize = aCLSize;
                        int lastACLSize = aCLSize;
                        while (difACLSize > 0) {
                            endDate.setTime(endDate.getTime() + (86400000 * difACLSize));
                            aCLSize = asistanceControlManager.sizeByEmployeePeriod(e, s.getInitDate(), endDate);
                            difACLSize = aCLSize - lastACLSize;
                            lastACLSize = aCLSize;
                        }
                        if (endDate.before(currentDate)) {
                            Date iD = new Date(endDate.getTime() + 86400000);
                            Date eD = new Date(iD.getTime());
                            int m = eD.getMonth() + 1;
                            if (m == 12) {
                                m = 0;
                            }
                            eD.setMonth(m);
                            if (eD.getMonth() > m) {
                                while (eD.getMonth() > m) {
                                    eD.setTime(eD.getTime() - 86400000);
                                }
                            } else {
                                eD.setTime(eD.getTime() - 86400000);
                            }
                            Salary newS = new Salary(false, false, iD, eD, e.getSalarieFixed(), e);
                            salaryManager.save(newS);
                            if (asistanceControlManager.sizeByHalfEmployeePeriod(e, s.getInitDate(), endDate)) {
                                asistanceControlManager.save(new AsistanceControl(iD, true, e));
                            }
                        }
                    } else {
                        Date currentDate = new Date(System.currentTimeMillis());
                        if (e.getEntryDate().equals(currentDate) || e.getEntryDate().before(currentDate)) {
                            Date eD = new Date(e.getEntryDate().getTime());
                            int m = eD.getMonth() + 1;
                            if (m == 12) {
                                m = 0;
                            }
                            eD.setMonth(m);
                            if (eD.getMonth() > m) {
                                while (eD.getMonth() > m) {
                                    eD.setTime(eD.getTime() - 86400000);
                                }
                            } else {
                                eD.setTime(eD.getTime() - 86400000);
                            }
                            Salary newS = new Salary(false, false, e.getEntryDate(), eD, e.getSalarieFixed(), e);
                            salaryManager.save(newS);
                        }
                    }
                }
            }
        }

        return SUCCESS;
    }
    private List<LabelValue> lvOptions = new ArrayList<LabelValue>();
    private List<LabelValue> lvRoles = new ArrayList<LabelValue>();
    private LabelValue currentLVRolUser = new LabelValue();
    private String currentNameUser = "";
    private User user = null;

    public String setupMenu() {
        setUpCurrentUser();
        getRequest().setAttribute("fullNameUser", user.getFullName());
        setUpRoles();
        getRequest().setAttribute("currentRoleUser", currentLVRolUser.getLabel());
        setupOptions();
        return SUCCESS;

    }

    public void setUpCurrentUser() {
        if (user == null) {
            currentNameUser = getRequest().getRemoteUser();
            user = this.userManager.getUserByUsername(currentNameUser);
        }

    }

    public void setUpRoles() {
        if (lvRoles.isEmpty()) {
            lvRoles = user.getRoleList();
            currentLVRolUser = lvRoles.get(0);
        }
    }

    public List<LabelValue> getLvRoles() {
        return lvRoles;
    }

    public String getNameCurrenUser() {
        return currentNameUser;
    }

    public String setupOptions() {
        lvOptions.clear();
        Role role = this.roleManager.getRole(currentLVRolUser.getValue());
        Iterator itOptions = role.getOptions().iterator();
        System.out.println(itOptions.toString());
        while (itOptions.hasNext()) {
            Option option = (Option) itOptions.next();
            System.out.println(option.getName());
            lvOptions.add(new LabelValue(option.getTitle(), option.getPage()));

        }
        return SUCCESS;
    }

    public LabelValue getCurrentLVRolUser() {
        return currentLVRolUser;
    }

    public void setCurrentLVRolUser(LabelValue currentLVRolUser) {
        this.currentLVRolUser = currentLVRolUser;
    }
}
